********************************************************************************
/*
This file collects inheritance transaction information to be used in APR

*/
********************************************************************************

* LOAD DATA
u $data/inheritance95_13, replace

drop year
gen year = substr(date_decision,7,4)
replace year = substr(date_possession,7,4) if year == ""
destring year, replace


keep inheritance lnr* description year

preserve
keep lnr* year
save $data/inheritance_event, replace
restore


collapse (sum) inheritance (firstnm) description lnr_giv, by(lnr_rec year)
label var inheritance ""
label var description ""
label var lnr_giv ""
rename lnr_rec lnr
label var lnr ""
save $data/inheritance95_13_clean, replace

u $data/inheritance95_13, replace
drop year
gen year = substr(date_decision,7,4)
replace year = substr(date_possession,7,4) if year == ""
destring year, replace
keep inheritance lnr* year

preserve
collapse (sum) inheritance, by(lnr_rec year)
rename lnr_rec lnr
gen beq_received = 1
save $data/inheritance_rec, replace
restore

preserve
collapse (sum) inheritance, by(lnr_giv year)
rename lnr_giv lnr
gen beq_given = 1
save $data/inheritance_giv, replace
restore


* make number of offspring dataset
u $data/lnr_mlnr_flnr_updated2013, replace
bys mlnr: gen mKids = _N
bys flnr: gen fKids = _N
preserve 
keep if mlnr != .
keep mlnr mKids
rename mlnr lnr 
rename mKids offspring
bys lnr: keep if _n ==1 
save $data/_temp_mkids, replace
restore

keep if flnr != .
keep flnr fKids
rename flnr lnr 
rename fKids offspring
bys lnr: keep if _n ==1 
append using $data/_temp_mkids

bys lnr: egen maxTemp = max(offspring)
replace offspring = maxTemp
bys lnr: keep if _n == 1
drop maxTemp
save $data/lnr_kids, replace



** death events
u $data/deaths19612016, replace
rename yod year
keep if year >= 1993 & year <= 2016
gen death = 1
save $data/lnr_death, replace
drop death


preserve
rename lnr flnr
merge 1:m flnr using $data/lnr_mlnr_flnr_updated2013, nogen keep(match) keepusing(lnr)
save $data/_temp_offspring_f, replace
restore

rename lnr mlnr
merge 1:m mlnr using $data/lnr_mlnr_flnr_updated2013, nogen keep(match) keepusing(lnr)
append using $data/_temp_offspring_f
gen fdeath = flnr != .
gen mdeath = mlnr != .
keep year lnr fdeath mdeath
gen parent_death = 1
bys lnr year: gen NNN = _N
replace fdeath = 1 if NNN == 2
replace mdeath = 1 if NNN == 2

* some may experience that both parents die in the same year
bys lnr year: keep if _n == 1
drop NNN
save $data/parent_death, replace







